import Vue from 'vue'
import Router from 'vue-router'
import menu from '@/views/menu'
import personnel from '@/views/personnel'
import login from '@/views/login'
import mine from '@/views/mine'
import test from '@/components/test'
import tabbar from '@/components/tabbar'
import { getToken } from '@/utils/auth';

Vue.use(Router)

let whiteList = ['/login', '/test'];

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'menu',
      component: tabbar,
      redirect: '/menu',
      children: [
        {
          path: '/menu',
          component: menu
        },
        {
          path: '/personnel',
          component: personnel
        },
        {
          path: '/mine',
          component: mine
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/test',
      name: 'test',
      component: test
    },
  ],
})
router.beforeEach((to, from, next) => {
  if (getToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next();
    }
  } else if (whiteList.indexOf(to.path) === -1) {
    next('/login');
    // next();
  } else {
    next();
  }
});
export default router;